package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import net.tsz.afinal.annotation.view.CodeNote;

import java.io.File;
import java.util.List;

import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.add.police.company.IRegUnitView;
import liuliu.throughwaring.control.add.police.company.RegUnitListener;
import liuliu.throughwaring.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;

/**
 * Created by liuliu on 2015/11/19   9:25
 * 注册民警信息
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class RegUnitActivity extends BaseActivity implements IRegUnitView {
    @CodeNote(id = R.id.reg_unit_name)
    ImageEditText reg_unit_name;//单位名称
    @CodeNote(id = R.id.reg_unit_corporation)
    ImageEditText reg_unit_corporation;//单位法人
    @CodeNote(id = R.id.reg_unit_security)
    ImageEditText txt_reg_unit_securitytel;//治安负责
    @CodeNote(id = R.id.reg_unit_address)
    ImageEditText reg_unit_address;//单位地址
    @CodeNote(id = R.id.reg_unit_btn, click = "onClick")
    Button reg_btn;//注册
    @CodeNote(id = R.id.reg_unit_tel)
    ImageEditText reg_unit_tel;//电话
    @CodeNote(id = R.id.reg_unit_remark)
    ImageEditText reg_unit_remark;//备注
    @CodeNote(id = R.id.reg_unit_img, click = "onClick")
    ImageView reg_unit_img;//单位照片
    @CodeNote(id = R.id.right_menu_police_search_ll)
    LinearLayout right_menu_police_search_ll;
    @CodeNote(id = R.id.right_menu_unit_search_ll)
    LinearLayout right_menu_unit_search_ll;
    @CodeNote(id = R.id.right_menu_person_search_ll)
    LinearLayout right_menu_person_search_ll;
    CompanyModel companyModel = new CompanyModel();
    RegUnitListener mListener;
    @CodeNote(id = R.id.reg_unit_toolbar)
    TToolbar toolbar;
    @CodeNote(id = R.id.reg_unit_btn_add, click = "onClick")
    Button btnAdd;
    Bitmap bm = null;
    StringBuffer path = new StringBuffer();
    String type;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_reg_unit);
        type = getIntent().getStringExtra("type");
        switch (type) {
            case "congye":
                toolbar.setCenterTitle("单位注册");
                companyModel.setCompanyType("Company");
                break;
            case "zulin":
                toolbar.setCenterTitle("出租屋注册");
                companyModel.setCompanyType("Lease");
                break;
        }
        mListener = new RegUnitListener(this, this);
        reg_unit_address.setText(Utils.ReadString("NowLocation"));
    }

    @Override
    public void initEvents() {
        //顶部菜单左侧点击
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
    }

    //清空数据
    private void ClearText() {
        companyModel = new CompanyModel();
        reg_unit_name.setText("");
        reg_unit_address.setText("");
        reg_unit_remark.setText("");
        txt_reg_unit_securitytel.setText("");
        reg_unit_tel.setText("");
        reg_unit_corporation.setText("");
        reg_unit_img.setImageResource(R.mipmap.main_add_img);
    }

    //判断
    private boolean checkText() {
        if (reg_unit_name.getText().equals("")) {
            Utils.ToastShort(RegUnitActivity.this, "单位名称不能为空!");
            return false;
        }

        if (reg_unit_corporation.getText().equals("")) {
            Utils.ToastShort(RegUnitActivity.this, "单位法人不能为空!");
            return false;
        }
        if (txt_reg_unit_securitytel.getText().equals("")) {
            Utils.ToastShort(RegUnitActivity.this, "治安负责不能为空!");
            return false;
        }
        if (reg_unit_tel.getText().equals("")) {
            Utils.ToastShort(RegUnitActivity.this, "联系电话不能为空!");
            return false;
        }
        if (reg_unit_address.getText().equals("")) {
            Utils.ToastShort(RegUnitActivity.this, "单位地址不能为空!");
            return false;
        }
        if (bm == null) {
            Utils.ToastShort(RegUnitActivity.this, "请为单位拍照!");
            return false;
        }
        return true;

    }

    //获取页面数据
    private void getText() {
        companyModel.setCompanyLegalPerson(reg_unit_corporation.getText());
        companyModel.setCompanyName(reg_unit_name.getText().trim());
        companyModel.setCompanyAddress(reg_unit_address.getText().trim());
        companyModel.setCompanyComment(reg_unit_remark.getText().trim());
        companyModel.setCompanySecurityPerson(txt_reg_unit_securitytel.getText().trim());
        companyModel.setCompanyLegalPerson(txt_reg_unit_securitytel.getText().trim());
        companyModel.setCompanyPhoneNumber(reg_unit_tel.getText().trim());
        if (bm != null) {
            companyModel.setCompanyOveralImage(Utils.encodeBitmap(bm));
        }
    }


    boolean saveAndadd = false;//保存并添加，true.false只是保存

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reg_unit_btn://保存按钮
                if (checkText()) {
                    getText();
                    mListener.RegUnit(companyModel);
                    saveAndadd = false;
                }
                break;
            case R.id.reg_unit_img:
                startCamera(11);
                break;
            case R.id.reg_unit_btn_add://保存并继续添加按钮
                if (checkText()) {
                    getText();
                    mListener.RegUnit(companyModel);
                    saveAndadd = false;
                }
                break;
        }
    }

    //开启拍照
    public void startCamera(int type) {
        // 利用系统自带的相机应用:拍照
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        path = new StringBuffer();
        path.append(RegUnitActivity.this.getExternalFilesDir(null)).append("/IMG_CASH.jpg");
        File file = new File(path.toString());
        Uri uri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, type);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == -1) {
            if (requestCode == 11) {
                bm = Utils.getimage(this, path.toString());
                if (bm != null) {
                    reg_unit_img.setImageBitmap(bm);
                    reg_unit_img.setTag(bm);
                } else {
                    reg_unit_img.setImageResource(R.mipmap.main_add_img);
                    reg_unit_img.setTag(null);
                    // policeImage.setImageBitmap(BitmapFactory.decodeResource(this.getResources(),R.mipmap.person_header));
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRegUnitResult(boolean isTrue, String mes) {
        if (saveAndadd) {//停留在本页，只是清空数据。
            ClearText();
        } else {//返回查询页面。并显示查询结果。
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("companyName", reg_unit_name.getText());//单位名称
            intent.putExtras(bundle);
            setResult(20, intent);
            this.finish();
        }
        mUtils.ToastShort("添加成功");
    }

    private String getCodeVlaue(List<CodeModel> list, String name) {
        for (CodeModel c : list) {
            if (c.getName().equals(name)) {
                return c.getValue();
            }
        }
        return "";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bm != null) {
            if (!bm.isRecycled()) {
                bm.recycle();//回收图片所占的内存
                System.gc();
            }
        }
    }
}
